//
//  YZPersonalCenterHeaderView.m
//  VET
//
//  Created by 刘文靖 on 2018/6/28.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZPersonalCenterHeaderView.h"
#import "UIImageView+CornerRadius.h"
#import "UIView+Frame.h"
#import "YZUser.h"
#import <UIImageView+WebCache.h>
#import "YZStatementHeader.h"
@interface YZPersonalCenterHeaderView()
@property (nonatomic, weak) UILabel *nickNameLabel;
@property (nonatomic, weak) UIImageView *avatarImageView;
@property (nonatomic, weak) UILabel *organizationLabel;
@property (nonatomic, weak) UIButton *editBtn;
@end
@implementation YZPersonalCenterHeaderView
#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        // UI
        [self setupGradientLayer];
        [self setupUI];
    }
    return self;
}

- (void)setupGradientLayer{
    
    CAGradientLayer *maskLayer = [CAGradientLayer layer];
    CGColorRef topGradientColor = yzColorGS.CGColor;
    CGColorRef bottomGradientColor = yzColorGE.CGColor;;
    maskLayer.colors = @[(__bridge id)topGradientColor, (__bridge id)bottomGradientColor];
    maskLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    [self.layer addSublayer:maskLayer];
    CALayer *imageLayer = [CALayer layer];
    imageLayer.frame = maskLayer.frame;
    imageLayer.contents = (__bridge id)[UIImage imageNamed:@"personal_header"].CGImage;
    [self.layer addSublayer:imageLayer];
}


- (void)setupUI{
    UIImageView *avatarImageView = [[UIImageView alloc] initWithCornerRadiusAdvance:23 rectCornerType:UIRectCornerAllCorners];
    [avatarImageView zy_attachBorderWidth:1.5 color:kColorWithHex(0xffffff)];
    avatarImageView.frame = CGRectMake((kScreenWidth - 70) * 0.5, 22 + kStatusBarHeight, 70, 70);
    [self addSubview:avatarImageView];
    self.avatarImageView = avatarImageView;
    
    UILabel *nickNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, avatarImageView.bottom + 17, kScreenWidth, 28)];
    nickNameLabel.textAlignment = NSTextAlignmentCenter;
    nickNameLabel.textColor =kColorWithHex(0xffffff);
    nickNameLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:28];
    [self addSubview:nickNameLabel];
    self.nickNameLabel = nickNameLabel;
    
    UILabel *organizationLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, nickNameLabel.bottom + 6, kScreenWidth, 12)];
    organizationLabel.textAlignment = NSTextAlignmentCenter;
    organizationLabel.textColor =kColorWithHex(0xffffff);
    organizationLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
    [self addSubview:organizationLabel];
    self.organizationLabel = organizationLabel;
    
    UIButton *editBtn  = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 58, 87, 70, 30)];
    editBtn.backgroundColor = kColorWithHex(0x30A557);
    editBtn.layer.cornerRadius = 15;
    [editBtn setImage:[UIImage imageNamed:@"personal_edit"] forState:UIControlStateNormal];
    [editBtn setTitle:@"编辑" forState:UIControlStateNormal];
    editBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
    [editBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -12, 0, 0)];
    [editBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -4, 0, 0)];
    [self addSubview:editBtn];
    self.editBtn = editBtn;
    @weakify(self)
    [[editBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [self personalInfoClicked];
    }];
}

- (void)personalInfoClicked{
    if (self.personalInfoClickedBlock) {
        self.personalInfoClickedBlock();
    }
}

- (void)refreshWithLoginUser:(YZUser *)loginUser{
    self.nickNameLabel.text = loginUser.nickName;
    self.organizationLabel.text = loginUser.company?:@"";
    [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:loginUser.avatarOriginal] placeholderImage:[UIImage imageNamed:@"icon_im"]];
}
@end
